package com.example.healthcode2;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
//第一个Activity
public class FirstActivity extends AppCompatActivity {
    private static final int SIGN_OK = 1;
    private static final int SIGN_NO = 2;
    private static final int SIGN_ERROR=3;
    private static final int OTHER=0;
    private String name,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }


        try {
            init();//初始化
        } catch (IOException e) {
            e.printStackTrace();
            Intent intent=new Intent(this,indexActivity.class);
            startActivity(intent);
            finish();
        }
    }



    private void init() throws IOException {
          name = readfile("name.txt");
          password=readfile("password.txt");//读取保存的 用户名和密码
        if (name.isEmpty()||password.isEmpty()){//如果没有用户名或密码的数据
            Intent intent=new Intent(this,indexActivity.class);//进入登录Activity
            startActivity(intent);
            finish();
        }else {//执行登录操作

            RequestQueue queue = Volley.newRequestQueue(this);
            String url = "http://30r97104b2.qicp.vip/user/login";
            StringRequest request = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            System.out.println(s);
                            if ("success".equals(s)) {
                                myHandler.sendEmptyMessage(SIGN_OK);
                            }else if ("error".equals(s)) {
                                myHandler.sendEmptyMessage(SIGN_NO);
                            }else {
                                myHandler.sendEmptyMessage(OTHER);
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            System.out.println(volleyError);
                            myHandler.sendEmptyMessage(SIGN_ERROR);
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    params.put("username", name);
                    params.put("password", password);
                    return params;
                }
            };
            queue.add(request);

        }
    }

    @SuppressLint("HandlerLeak")
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case SIGN_OK://登录成功进入主页面

                    Intent intent = new Intent(FirstActivity.this, MainActivity.class);
                    intent.putExtra("name",name);
                    startActivity(intent);
                    finish();
                    break;
                default:
                    Intent it = new Intent(FirstActivity.this, indexActivity.class);//进入登录界面
                    startActivity(it);
                    finish();
                    break;


            }
        }
    };
//读取文件
    private String readfile(String filename) throws IOException {
        InputStream in = openFileInput(filename);
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();
        int len = -1;
        while ((len = in.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }
        in.close();
        return sb.toString();
    }
}
